spring mvc3.1 @ResponseBody注解生成大量Accept-Charset - 赠人玫瑰,手留余香. - 博客频道 - CSDN.NET

创建时间:2017/4/24 15:53
来源:http://blog.csdn.net/sunshine_bean/article/details/40192253


分类:
Spring mvc(9)
.

版权声明:本文为博主原创文章,未经博主允许不得转载。

Spring3 MVC使用@ResponseBody后会产生很大的响应头(Accept-Charset会达到4K+),原因在于默认情况下StringHttpMessageConverter.writeInternal()会将所有可用字符集回写到response响应头中:问题来了


解决方式:

一般我们都会重写springs mvc的HttpMessageConverter,改为utf-8编码:

  1. package com.goldpalm.core.spring.mvc;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStreamReader;  
  5. import java.io.OutputStreamWriter;  
  6. import java.io.UnsupportedEncodingException;  
  7. import java.nio.charset.Charset;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10.   
  11. import org.springframework.http.HttpInputMessage;  
  12. import org.springframework.http.HttpOutputMessage;  
  13. import org.springframework.http.MediaType;  
  14. import org.springframework.http.converter.AbstractHttpMessageConverter;  
  15. import org.springframework.util.FileCopyUtils;  
  16.   
  17. /** 
  18.  * 重写SpringMVC的字符串转换器,使用UTF-8编码 
  19.  * @since 2012-7-5 下午2:28:19 
  20.  * @author Jesse Lu 
  21.  */  
  22. public class UTF8StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {  
  23.       
  24.     public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");  
  25.       
  26.     private final List<Charset> availableCharsets;  
  27.       
  28.     private boolean writeAcceptCharset = true;  
  29.       
  30.     public UTF8StringHttpMessageConverter() {  
  31.         super(new MediaType("text""plain", DEFAULT_CHARSET), MediaType.ALL);  
  32.         this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());  
  33.     }  
  34.       
  35.     /** 
  36.      * Indicates whether the {@code Accept-Charset} should be written to any outgoing request. 
  37.      * <p> 
  38.      * Default is {@code true}. 
  39.      */  
  40.     public void setWriteAcceptCharset(boolean writeAcceptCharset) {  
  41.         this.writeAcceptCharset = writeAcceptCharset;  
  42.     }  
  43.       
  44.     @Override  
  45.     public boolean supports(Class<?> clazz) {  
  46.         return String.class.equals(clazz);  
  47.     }  
  48.       
  49.     @SuppressWarnings("rawtypes")  
  50.     @Override  
  51.     protected String readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException {  
  52.         Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());  
  53.         return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));  
  54.     }  
  55.       
  56.     @Override  
  57.     protected Long getContentLength(String s, MediaType contentType) {  
  58.         Charset charset = getContentTypeCharset(contentType);  
  59.         try {  
  60.             return (long) s.getBytes(charset.name()).length;  
  61.         } catch (UnsupportedEncodingException ex) {  
  62.             // should not occur  
  63.             throw new InternalError(ex.getMessage());  
  64.         }  
  65.     }  
  66.       
  67.     @Override  
  68.     protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException {  
  69.         if (writeAcceptCharset) {  
  70.             outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());  
  71.         }  
  72.         Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());  
  73.         FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset));  
  74.     }  
  75.       
  76.     /** 
  77.      * Return the list of supported {@link Charset}. 
  78.      * <p> 
  79.      * By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses. 
  80.      * @return the list of accepted charsets 
  81.      */  
  82.     protected List<Charset> getAcceptedCharsets() {  
  83.         return this.availableCharsets;  
  84.     }  
  85.       
  86.     private Charset getContentTypeCharset(MediaType contentType) {  
  87.         if (contentType != null && contentType.getCharSet() != null) {  
  88.             return contentType.getCharSet();  
  89.         } else {  
  90.             return DEFAULT_CHARSET;  
  91.         }  
  92.     }  
  93.       
  94. }  

在xm中配置:注意红色圈起来的配置



  1. <mvc:annotation-driven>  
  2.         <mvc:message-converters>  
  3.             <bean class="com.goldpalm.core.spring.mvc.UTF8StringHttpMessageConverter">  
  4.                 <property name="writeAcceptCharset" value="false" />  
  5.             </bean>  
  6.         </mvc:message-converters>  
  7.     </mvc:annotation-driven>  





.
0
0
.

我的同类文章

Spring mvc(9)